﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class inventory_static_grid : MonoBehaviour
{
    public int id = 0;
    public static int EMPTY = 0;
    public static int MESS_MAX = -2;
    public static int DIFF = -1;
    public static int OKAY = 1;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    
    }

    public int Can_Be_Add(int id)
    {
        if (this.id == 0) return EMPTY;
        else
        {
            if (this.id != id) return DIFF;
            else
            {
                inventory_movable_item item = transform.GetComponentInChildren<inventory_movable_item>();
                if (item.GetObjectinfo().str_type == Objecttype.equipment)
                {
                    return MESS_MAX;
                }
                else return OKAY;
             }

        }
    }

    public void SetId(int id, int num = 1)
    {
        this.id = id;

        if(id != 0)
        {
            inventory_movable_item item_sprite = this.GetComponentInChildren<inventory_movable_item>();
            if (item_sprite != null) item_sprite.SetId(id, num);
            else
            {
                Debug.Log("创建新的内容");
                item_sprite = Instantiate(Inventory._instance.Item_prefabs, this.transform).GetComponent<inventory_movable_item>();
                item_sprite.SetId(id, num);
            }
        }
    }

    public void Set_Object(Objectinfo obj, int num = 1)
    {
        this.id = obj.id;

        inventory_movable_item item_sprite = this.GetComponentInChildren<inventory_movable_item>();
        if (item_sprite != null) item_sprite.Set_Prop(obj, num);
        else
        {
            Debug.Log("创建新的物体");
            item_sprite = Instantiate(Inventory._instance.Item_prefabs, this.transform).GetComponent<inventory_movable_item>();
            item_sprite.Set_Prop(obj, num);
        }
    }

    public void ChangeId(int id)
    {
        this.id = id;
    }

    public int Get_Cure_Point()
    {
        if (id == 0) return 0;
        else
        {
            inventory_movable_item item = transform.GetComponentInChildren<inventory_movable_item>();
            Objectinfo info = item.GetObjectinfo();
            if (info.str_type == Objecttype.drug) return info.hp;
            else return 0;
        }
    }

    public void Set_Prop(Objectinfo a_prop, int num = 1)
    {
        this.id = a_prop.id;

        inventory_movable_item item_sprite = this.GetComponentInChildren<inventory_movable_item>();
        item_sprite.SetId(this.id);
      
    }

    // 清空所有的格子的信息
    public void Clear_Grid()
    {
        // 把对应的格子删除了
        if(id != 0)
        {
            id = 0;
        }
    }
    // 获得这个格子的重量
    public int Get_Wight()
    {
        if (id == 0) return 0;
        else
        {
            inventory_movable_item Item_in_grid = transform.GetComponentInChildren<inventory_movable_item>();
            return Item_in_grid.Get_Weight();
        }
    }
    // 让这个格子的道具被使用
    public void acitivate_Prop()
    {
        if(id == 0)
        {
            return;
        }else
        {
            inventory_movable_item item = GetComponentInChildren<inventory_movable_item>();
            if(item == null)
            {
                id = 0;
                Debug.Log("inventery error");
                return;
            }
            else
            {
                item.activate();

            }
        }
    }

    public inventory_movable_item Get_item_inside()
    {
        if (id != 0) return this.GetComponentInChildren<inventory_movable_item>();
        else return null;
    }

}
